package com.logistics.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.logistics.common.core.constant.Constants;
import com.logistics.common.core.utils.DateUtils;
import com.logistics.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.logistics.system.mapper.TransportPlansMapper;
import com.logistics.system.domain.TransportPlans;
import com.logistics.system.service.ITransportPlansService;

/**
 * 运输计划Service业务层处理
 * 
 * @author logistics
 * @date 2024-06-23
 */
@Service
public class TransportPlansServiceImpl implements ITransportPlansService 
{
    @Autowired
    private TransportPlansMapper transportPlansMapper;

    /**
     * 查询运输计划
     * 
     * @param planId 运输计划主键
     * @return 运输计划
     */
    @Override
    public TransportPlans selectTransportPlansByPlanId(Long planId)
    {
        return transportPlansMapper.selectTransportPlansByPlanId(planId);
    }

    /**
     * 查询运输计划列表
     * 
     * @param transportPlans 运输计划
     * @return 运输计划
     */
    @Override
    public List<TransportPlans> selectTransportPlansList(TransportPlans transportPlans)
    {
        return transportPlansMapper.selectTransportPlansList(transportPlans);
    }

    /**
     * 新增运输计划
     * 
     * @param transportPlans 运输计划
     * @return 结果
     */
    @Override
    public int insertTransportPlans(TransportPlans transportPlans)
    {
        transportPlans.setPlanStatus(Constants.PLAN_STATUS_0);
        transportPlans.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        transportPlans.setCreateTime(DateUtils.getNowDate());
        return transportPlansMapper.insertTransportPlans(transportPlans);
    }

    /**
     * 修改运输计划
     * 
     * @param transportPlans 运输计划
     * @return 结果
     */
    @Override
    public int updateTransportPlans(TransportPlans transportPlans)
    {
        transportPlans.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        transportPlans.setUpdateTime(DateUtils.getNowDate());
        return transportPlansMapper.updateTransportPlans(transportPlans);
    }

    /**
     * 批量删除运输计划
     * 
     * @param planIds 需要删除的运输计划主键
     * @return 结果
     */
    @Override
    public int deleteTransportPlansByPlanIds(Long[] planIds)
    {
        return transportPlansMapper.deleteTransportPlansByPlanIds(planIds);
    }

    /**
     * 删除运输计划信息
     * 
     * @param planId 运输计划主键
     * @return 结果
     */
    @Override
    public int deleteTransportPlansByPlanId(Long planId)
    {
        return transportPlansMapper.deleteTransportPlansByPlanId(planId);
    }

    /**
     * 查询某个合同创建出来的计划的运输量总和
     * @param contractId 合同id
     * @return
     */
    @Override
    public BigDecimal sumTransportVolume(Long contractId) {
        BigDecimal result = transportPlansMapper.sumTransportVolume(contractId);
        if(ObjectUtils.isEmpty(result)){
            return BigDecimal.ZERO;
        }
        return result;
    }

    @Override
    public Long selectContractIdByWaybillId(Long waybillId) {

        return transportPlansMapper.selectContractIdByWaybillId(waybillId);
    }


}
